//
//  DataPartView.m
//  lvtu
//
//  Created by Apple on 12-3-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DataPartView.h"
#import "TripInfo.h"
#import "EGOImageView.h"
#import "EventThumbViem.h"

@implementation DataPartView

@synthesize partLabel;
@synthesize eventCountLabel;
@synthesize editButton;
@synthesize addButton;
@synthesize delegate;
@synthesize elementType;
@synthesize eventArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        inputView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , self.frame.size.height+10)];
        UIImage* stretchImg = [[UIImage imageNamed:@"bg_whiteframe.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:10];
        inputView.image = stretchImg;
        inputView.clipsToBounds = YES;
        inputView.userInteractionEnabled = YES;
        [self addSubview:inputView];
        
        partLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 14, 60, 20)];
		partLabel.backgroundColor = [UIColor clearColor];
		partLabel.text = @"早上：";
        partLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
		[partLabel setFont:[UIFont systemFontOfSize:12]];
		[inputView addSubview:partLabel];
        
        eventCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 14, 180, 20)];
		eventCountLabel.backgroundColor = [UIColor clearColor];
        eventCountLabel.textAlignment = UITextAlignmentCenter;
		eventCountLabel.text = @"0事件";
        eventCountLabel.textColor = [UIColor colorWithRed:101/255.0 green:101/255.0 blue:101/255.0 alpha:1.0];
		[eventCountLabel setFont:[UIFont systemFontOfSize:17]];
		[inputView addSubview:eventCountLabel];
        
        addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addButton.frame = CGRectMake(250, 10, 30, 30);
        [addButton setBackgroundImage:[UIImage imageNamed:@"btn_add_normal.png"] forState:UIControlStateNormal];
        [addButton setBackgroundImage:[UIImage imageNamed:@"btn_add_press.png"] forState:UIControlStateHighlighted];
        [addButton addTarget:self action:@selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [inputView addSubview:addButton];
        
        editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        editButton.frame = CGRectMake(250, 10, 30, 30);
        [editButton setBackgroundImage:[UIImage imageNamed:@"btn_edit_normal.png"] forState:UIControlStateNormal];
        [editButton setBackgroundImage:[UIImage imageNamed:@"btn_edit_press.png"] forState:UIControlStateHighlighted];
        [editButton addTarget:self action:@selector(editButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [inputView addSubview:editButton];
        editButton.hidden = YES;
        
        eventArray = [[NSMutableArray alloc] init];
//        eventScorllView = nil;
        eventScorllView = [[UIScrollView alloc] init];
        eventScorllView.backgroundColor = [UIColor clearColor];
        eventScorllView.scrollsToTop = YES;
        eventScorllView.showsHorizontalScrollIndicator = NO;
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [inputView setFrame:CGRectMake(0, 0, self.frame.size.width , self.frame.size.height+10)];
}

- (void)addButtonPressed:(id)sender
{
    [delegate touchUpInsideEditViewCell:partLabel.text button:0 elementType:elementType];
}

- (void)editButtonPressed:(id)sender
{
    [delegate touchUpInsideEditViewCell:partLabel.text button:1 elementType:elementType];
}

- (void)dealloc
{
    if (lineView) {
        [lineView release];lineView = nil;
    }
    [partLabel release];
    [eventCountLabel release];
    [addButton release];
    [editButton release];
    [inputView release];
    
    if ([eventArray count] > 0) {
        [eventArray removeAllObjects];
        [eventArray release]; eventArray = nil;
        [eventScorllView release]; eventScorllView = nil;
    }
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)setEventCountForEdit:(NSInteger) count
{
    eventCountLabel.text = [NSString stringWithFormat:@"%d事件",count];
    if (count > 0) {
        [self setButtonStyle:BOTHBUTTON];
    }
    else
    {
        [self setButtonStyle:ADDBUTTON];
    }
}

- (void)setEventCount:(NSInteger) count
{
    eventCountLabel.text = [NSString stringWithFormat:@"%d事件",count];
    if (count > 0) {
        [self setButtonStyle:EDITBUTTON];
    }
    else
    {
        [self setButtonStyle:ADDBUTTON];
    }
}

- (void)setButtonStyle:(BUTTON_STYLE) style
{
    if (style == ADDBUTTON) {
        addButton.frame = CGRectMake(250, 10, 30, 30);
        addButton.hidden = NO;
        editButton.hidden = YES;
    }
    else if (style == EDITBUTTON)
    {
        editButton.frame = CGRectMake(250, 10, 30, 30);
        addButton.hidden = YES;
        editButton.hidden = NO;
    }
    else if (style == BOTHBUTTON)
    {
        editButton.frame = CGRectMake(220, 10, 30, 30);
        editButton.hidden = NO;
        addButton.frame = CGRectMake(250, 10, 30, 30);
        addButton.hidden = NO;
    }
    else
    {
        addButton.hidden= YES;
        editButton.hidden = YES;
    }
}

- (BOOL)setEventShow:(NSMutableArray*) array
{
    if ([array count] > 0) {
        if (!lineView) {
            lineView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 45, 260, 1)];
            lineView.tag = 50001;
            lineView.backgroundColor = [UIColor colorWithRed:217/255.0 green:218/255.0 blue:210/255.0 alpha:1.0];
            [inputView addSubview:lineView];
        }
        
        if ([array count] >= [eventArray count]) {
            NSMutableArray* shouldKeepArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < [array count];i++) {
                for (int j = 0; j < [eventArray count]; j++) {
                    if ([eventArray objectAtIndex:j] == [array objectAtIndex:i] ) {
                        [shouldKeepArray addObject:[eventArray objectAtIndex:j]];
                        break;
                    }
                }
            }
            [eventArray removeObjectsInArray:shouldKeepArray];
            [eventArray addObjectsFromArray:array];
            
            [shouldKeepArray removeAllObjects];
            [shouldKeepArray release]; shouldKeepArray = nil;
        }
        else
        {
            NSMutableArray* shouldelArray = [[NSMutableArray alloc] init];
            NSMutableArray* needAddArray = [NSMutableArray arrayWithArray:array];
            for (int i = 0; i < [eventArray count];i++) {
                BOOL isDel = YES;
                for (int j = 0; j < [needAddArray count]; j++) {
                    if ([eventArray objectAtIndex:i] == [needAddArray objectAtIndex:j] ) {
                        isDel = NO;
                        [needAddArray removeObjectAtIndex:j];
                        break;
                    }
                }
                if (isDel == YES) {
                    [shouldelArray addObject:[eventArray objectAtIndex:i]];
                }
            }
            [eventArray removeObjectsInArray:shouldelArray];
            [eventArray addObjectsFromArray:needAddArray];
            
            [shouldelArray removeAllObjects];
            [shouldelArray release]; shouldelArray = nil;
        }
        
        if (![eventScorllView isDescendantOfView:inputView]) {
//            inputView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width , self.frame.size.height)];
//            eventScorllView = [[UIScrollView alloc] initWithFrame:CGRectMake(15, 50, 290, 105)];
            [eventScorllView setFrame:CGRectMake(15, 50, self.frame.size.width - 30, 105)];
            [inputView addSubview:eventScorllView];
        }
//        [eventScorllView setContentSize:CGSizeMake([eventArray count]*110, 105)];
        NSLog(@"%d",[eventScorllView.subviews count]);
        NSMutableArray* removeArray = [NSMutableArray array];
        for (int i = 0; i< [eventScorllView.subviews count]; i++) {
            id view = [eventScorllView.subviews objectAtIndex:i];
            if ([view isKindOfClass:[EGOImageView class]] || [view isKindOfClass:[EventThumbViem class]])
            {
                [removeArray addObject:view];
            }
        }
        for (int i = 0; i < [removeArray count]; i++) {
            id view = [removeArray objectAtIndex:i];
            [view removeFromSuperview];
        }
        
        
        for (int i = 0; i < [eventArray count]; i++) {
            [eventScorllView setContentSize:CGSizeMake(5+(100+2)*[eventArray count], 105)];
            if ([[eventArray objectAtIndex:i] isKindOfClass:[PlaceInfo class]]) {
                PlaceInfo* info = [eventArray objectAtIndex:i];
//                EGOImageView* ImageView = [[EGOImageView alloc] initWithFrame:
//                                           CGRectMake(5 + (70+2)*i, 5, 70, 70)];
//                [ImageView setImageURL:[NSURL URLWithString:info.imageurl]];
//                [eventScorllView addSubview:ImageView];
//                [ImageView release];
//                
//                UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(5 + (70+2)*i, 65, 70, 30)];
//                if ([info.name rangeOfString:@"("].length > 0) {
//                    label.text = [info.name substringToIndex:[info.name rangeOfString:@"("].location];
//                }
//                else {
//                    label.text = info.name;
//                }
//                label.font = [UIFont systemFontOfSize:12];
//                label.textAlignment = UITextAlignmentCenter;
//                label.backgroundColor = [UIColor clearColor];
//                [eventScorllView addSubview:label];
//                [label release];
                EventThumbViem* view = [[EventThumbViem alloc] initWithFrame:CGRectMake((100+2)*i, 5, 100, 85) Data:info];
                [eventScorllView addSubview:view];
                [view release];
            }
            else
            {
                PlaceInfo* info = [eventArray objectAtIndex:i];
//                EGOImageView* ImageView = [[EGOImageView alloc] initWithFrame:
//                                           CGRectMake(5 + (70+2)*i, 5, 70, 70)];
//                [ImageView setImageURL:[NSURL URLWithString:[info objectForKey:@"image"]]];
//                [eventScorllView addSubview:ImageView];
//                [ImageView release];
//                
//                UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(5 + (70+2)*i, 65, 70, 30)];
//                NSString* name = [info objectForKey:@"name"];
//                if ([name rangeOfString:@"("].length > 0) {
//                    label.text = [name substringToIndex:[name rangeOfString:@"("].location];
//                }
//                else {
//                    label.text = name;
//                }
//                label.font = [UIFont systemFontOfSize:12];
//                label.textAlignment = UITextAlignmentCenter;
//                label.backgroundColor = [UIColor clearColor];
//                [eventScorllView addSubview:label];
//                [label release];
                EventThumbViem* view = [[EventThumbViem alloc] initWithFrame:CGRectMake((100+2)*i, 5, 100, 85) Data:info];
                [eventScorllView addSubview:view];
                [view release];
            }
        }
        return YES;
    }
    else
    {
        if ([eventScorllView isDescendantOfView:inputView]) {
            for (int k = 0; k < [inputView.subviews count]; k++) {
                UIView* view = [inputView.subviews objectAtIndex:k];
                if (view.tag == 50001) {
                    [view removeFromSuperview];
                }
            }
            NSMutableArray* removeArray = [NSMutableArray array];
            for (int i = 0; i< [eventScorllView.subviews count]; i++) {
                id view = [eventScorllView.subviews objectAtIndex:i];
                if ([view isKindOfClass:[EGOImageView class]] || [view isKindOfClass:[UILabel class]])
                {
                    [removeArray addObject:view];
                }
            }
            for (int i = 0; i < [removeArray count]; i++) {
                id view = [removeArray objectAtIndex:i];
                [view removeFromSuperview];
            }
            [eventArray removeAllObjects];
            [eventScorllView removeFromSuperview];
        }
        return NO;
    }
}
@end
